import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'

// 修改路由配置，确保每个路由都有组件
const routes = [
  {
    path: '/',
    component: () => import('../layouts/MainLayout.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',  // 这样会生成 /dashboard
        name: 'Dashboard',
        component: () => import('../views/Dashboard.vue'),
        meta: { title: '首页', icon: 'HomeFilled', roles: ['admin', 'teacher', 'reviewer'] }
      },
      {
        path: 'user',  // 这样会生成 /user
        name: 'User',
        component: () => import('../views/UserManagement.vue'),
        meta: { title: '用户管理', icon: 'User', roles: ['admin'] }
      },
      {
        path: 'teacher',
        name: 'Teacher',
        component: () => import('../views/TeacherManagement.vue'), // 使用空页面组件
        meta: { title: '教师管理', icon: 'UserFilled', roles: ['admin'] }
      },
      {
        path: 'research',
        name: 'Research',
        component: () => import('../layouts/BlankLayout.vue'),
        meta: { title: '科研数据管理', icon: 'Files', roles: ['admin', 'teacher', 'reviewer'] },
        redirect: '/research/project',
        children: [
          {
            path: 'project',
            name: 'Project',
            component: () => import('../views/ProjectManagement.vue'), // 使用空页面组件
            meta: { title: '科研项目', roles: ['admin', 'teacher', 'reviewer'] }
          },
          {
            path: 'paper',
            name: 'Paper',
            component: () => import('../views/PaperManagement.vue'), // 使用空页面组件
            meta: { title: '论文', roles: ['admin', 'teacher', 'reviewer'] }
          },
          {
            path: 'patent',
            name: 'Patent',
            component: () => import('../views/PatentManagement.vue'), // 使用空页面组件
            meta: { title: '专利', roles: ['admin', 'teacher', 'reviewer'] }
          },
          {
            path: 'award',
            name: 'Award',
            component: () => import('../views/AwardManagement.vue'), // 使用空页面组件
            meta: { title: '获奖', roles: ['admin', 'teacher', 'reviewer'] }
          }
        ]
      },
      {
        path: 'review',
        name: 'Review',
        component: () => import('../views/DataReview.vue'), // 使用空页面组件
        meta: { title: '数据审核', icon: 'Check', roles: ['admin', 'reviewer'] }
      },
      {
        path: 'rules',
        name: 'Rules',
        component: () => import('../views/ScoreRule.vue'), // 使用空页面组件
        meta: { title: '计算规则', icon: 'Setting', roles: ['admin'] }
      },
      // 在路由配置中修改个人信息页面的meta信息
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../views/Profile.vue'),
        meta: { 
          title: '个人信息', 
          icon: 'User', 
          roles: ['admin', 'teacher', 'reviewer'],
          hideInMenu: true // 在菜单中隐藏
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: { title: '注册' }
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // Use the imported store instead of requiring it
  const userStore = useUserStore()
  const token = userStore.token || localStorage.getItem('token')
  
  // 保存当前路径作为活动菜单（除了profile页面）
  if (to.path !== '/profile' && !to.path.includes('/login') && !to.path.includes('/register')) {
    localStorage.setItem('lastActiveMenu', to.path)
  }
  
  // 其他逻辑保持不变
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    if (!token) {
      next('/login')
    } else {
      const userRole = userStore.userRole || localStorage.getItem('userRole')
      
      // 检查用户是否有权限访问该路由
      if (to.meta.roles && !to.meta.roles.includes(userRole)) {
        next('/dashboard') // 无权限则跳转到首页
      } else {
        next()
      }
    }
  }
})

export default router

// 删除这段独立的路由对象
